package com.example.studentapp.ui;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.studentapp.R;
import com.example.studentapp.adapters.DiaryAdapter;
import com.example.studentapp.beans.Diary;
import com.example.studentapp.db.SQLiteDB;
import com.example.studentapp.diary.AddDiaryActivity;
import com.example.studentapp.diary.SearchDiaryActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public class DiaryFragment extends Fragment {

    ImageView diary_searchIV,diary_addIV;
    TextView diary_warnTV;
    ListView diaryLV;
    SQLiteDB sqLiteDB;
    DiaryAdapter diaryAdapter;
    SQLiteDatabase sqLiteDatabase;
    List<Diary> list;


    @Override
    @SuppressLint("Range")
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View root = inflater.inflate(R.layout.fragment_diary,container,false);
        diary_searchIV = root.findViewById(R.id.diary_searchIV);
        diary_addIV = root.findViewById(R.id.diary_addIV);
        diary_warnTV = root.findViewById(R.id.diary_warnTV);
        diaryLV = root.findViewById(R.id.diaryLV);

        list = new ArrayList<>();

        // 数据获取
        sqLiteDB = new SQLiteDB(getActivity());
        sqLiteDatabase = sqLiteDB.getWritableDatabase();
        String sql = "select * from diary order by record_date desc";
        Cursor cursor = sqLiteDatabase.rawQuery(sql, null);
        if(cursor.getCount()>0){
            cursor.moveToFirst();
            for(int i = 0; i<cursor.getCount();i++){
                 String title = cursor.getString(cursor.getColumnIndex("title"));
                int id = cursor.getInt(cursor.getColumnIndex("id"));
                String weather = cursor.getString(cursor.getColumnIndex("weather"));
                String record_date = cursor.getString(cursor.getColumnIndex("record_date"));
                list.add(new Diary(id, title, weather, record_date));
                cursor.moveToNext();
            }
            diaryAdapter = new DiaryAdapter(getActivity(),list);
            diaryLV.setAdapter(diaryAdapter);
            diary_warnTV.setVisibility(View.GONE);
            diaryLV.setVisibility(View.VISIBLE);
        }else {
            diary_warnTV.setVisibility(View.VISIBLE);
            diaryLV.setVisibility(View.GONE);
        }
        // 添加日记
        diary_addIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), AddDiaryActivity.class);
                startActivity(intent);

            }
        });

        // 日记列表
        diaryLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Dialog myDialog = new Dialog(getActivity());
                myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                Window window = myDialog.getWindow();
                window.setContentView(R.layout.del_course_dialog);
                myDialog.show();
                Button course_okBtn = window.findViewById(R.id.course_del_okBtn);
                Button course_cancelBtn = window.findViewById(R.id.course_del_cancelBtn);
                course_cancelBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        myDialog.dismiss();
                    }
                });
                course_okBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        try {
                            String del_sql = "delete from diary where id=" + list.get(i).getId();
                            sqLiteDatabase.execSQL(del_sql);
                            list.remove(i);
                            diaryAdapter.notifyDataSetChanged();
                            myDialog.dismiss();
                            Toast.makeText(getActivity(), getString(R.string.diary_del_ok), Toast.LENGTH_SHORT).show();

                        } catch (Exception e) {
                            Toast.makeText(getActivity(), getString(R.string.diary_del_failed), Toast.LENGTH_SHORT).show();
                        }

                    }
                });
            }
        });
        diary_searchIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getContext(), SearchDiaryActivity.class);
                startActivity(intent);
            }
        });



        return root;

    }
}